package com.ai.platform.system.service.impl;

import com.ai.platform.model.dto.Input;
import com.ai.platform.model.dto.Output;
import com.ai.platform.core.exception.BusinessException;
import com.ai.platform.system.entity.SysPermission;
import com.ai.platform.system.mapper.SysPermissionMapper;
import com.ai.platform.system.mapper.SysRoleMapper;
import com.ai.platform.system.service.ISysPermissionService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author yeyingsheng
 * @since 2018-05-12
 */
@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements ISysPermissionService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public void queryPermissionByRoleId(Input input, Output output)  throws BusinessException {
        Integer roleId=Integer.parseInt(input.getValue("roleId"));
        try {
            List<SysPermission> sysPermissionList = baseMapper.queryPermissionByRoleId(roleId);
            output.setData(sysPermissionList);
        } catch (SQLException e) {
            throw new BusinessException("SQL异常:"+e.getMessage());
        }
    }

    @Override
    public void listPermissionAll(Input input, Output output) throws BusinessException {
        //查询所有权限
        List<SysPermission> sysPermissionList = baseMapper.selectList(new EntityWrapper<SysPermission>().orderBy("order_no", true));
        //根据角色ID查询对应权限
        String roleId=input.getValue("roleId");

        //查询角色拥有权限
        List<SysPermission> mySysPermission=null;
        try {
            if(!StringUtils.isEmpty(roleId)){
              //查询角色对应的权限列表
              mySysPermission=baseMapper.queryPermissionByRoleId(Integer.parseInt(roleId));
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new BusinessException("SQL异常:"+e.getMessage());
        }
        List<Map<String, Object>> resultList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(sysPermissionList)){
            for(SysPermission sysPermission:sysPermissionList){
                boolean isChecked = false;
                if(!CollectionUtils.isEmpty(mySysPermission)) {
                    for (SysPermission myPermission : mySysPermission) {
                        if (sysPermission.getId().toString().equals(myPermission.getId().toString())) {
                            isChecked = true;
                            break;
                        }
                    }
                }
                Map<String, Object> map = new HashMap<>();
                map.put("id", sysPermission.getId());
                map.put("pid", sysPermission.getPid());
                map.put("name", sysPermission.getName());
                map.put("module", sysPermission.getModule());
                map.put("privilege", sysPermission.getPrivilege());
                map.put("checked", isChecked);
                resultList.add(map);
            }
        }
        output.setData(resultList);
    }
}
